package com.dreamo.pache;

import android.app.Activity;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.Surface;
import android.widget.FrameLayout;

/**
 * Activity responsible for scanning the parking ticket and pass the image to the OCR engine for
 * parsing.
 *
 * @author steve.jiang@gmail.com (Steve Jiang)
 */
public class ScanActivity extends Activity {
    private Camera mCamera;
    private int mCameraId;
    private TicketPreview mPreview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ticket_preview);

        // Create our Preview view and set it as the content of our activity.
        mPreview = new TicketPreview(this);
        FrameLayout preview = (FrameLayout) findViewById(R.id.ticket_preview);
        preview.addView(mPreview);
    }

    @Override
    protected void onResume() {
        super.onResume();
        openCamera();
        adjustCameraOrientation(this, mCameraId, mCamera);

        mPreview.setCamera(mCamera);
    }

    @Override
    protected void onPause() {
        super.onPause();
        // Release the camera immediately on pause event.
        releaseCamera();
    }


    /** Opens the front-facing camera instance if available to the app. */
    private void openCamera() {
        try {
            // Attempt to open the front facing camera.
            for (int cameraId = 0; cameraId < Camera.getNumberOfCameras(); cameraId++) {
                Camera.CameraInfo info = new Camera.CameraInfo();
                Camera.getCameraInfo(cameraId, info);
                if (info.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
                    mCameraId = cameraId;
                    mCamera = Camera.open(cameraId);
                    break;
                }
            }
        } catch (Exception e) {
            // Camera is not available (in use or does not exist)
            // TODO(gjiang): Create centralized tags for logs.
            Log.e("tag_s", "Cannot open camera");
        }
    }

    /**
     * Adjust the camera orientation based on the activity orientation.
     * TODO(gjiang): Refactor candidate, move to a util class.
     *
     * @param activity Activity in which the camera will be used.
     * @param cameraId Id of the camera.
     * @param camera Camera resource in use.
     */
    private static void adjustCameraOrientation(Activity activity, int cameraId, Camera camera) {
        Camera.CameraInfo info = new Camera.CameraInfo();
        int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
        int degrees = 0;

        Camera.getCameraInfo(cameraId, info);

        switch (rotation) {
            case Surface.ROTATION_0: degrees = 0; break;
            case Surface.ROTATION_90: degrees = 90; break;
            case Surface.ROTATION_180: degrees = 180; break;
            case Surface.ROTATION_270: degrees = 270; break;
        }

        int result;
        if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
            result = (info.orientation + degrees) % 360;
            // Mirror compensation.
            result = (360 - result) % 360;
        } else {
            result = (info.orientation - degrees + 360) % 360;
        }
        camera.setDisplayOrientation(result);
    }

    /** Release the camera for other apps. */
    private void releaseCamera() {
        if (mCamera != null) {
            mCamera.release();
            mCamera = null;
        }
    }
}
